import axios from 'axios';

console.log(process.env.NODE_ENV);

const base=process.env.NODE_ENV==='development'?
  'http://localhost:8000':
  '/bookshelf-backend/bookshelf/public';

export function getBooks(jdId, ddId, page=1, sortMethod, desc) {
  return axios.get(base+'/api/books', {
    params: {
      jd_id: jdId, dd_id: ddId, page, sort_method: sortMethod, desc,
    },
  }).then((result)=>result.data);
}

export function getBook(isbn) {
  return axios.get(base+'/api/book', {
    params: {
      isbn,
    },
  }).then((result)=>result.data);
}

export function getSpidersByKey(key) {
  return axios.get(base+'/api/spiders_by_key', {
    params: {
      key,
    },
  }).then((result)=>result.data);
}

export function getSpiders(page=1) {
  return axios.get(base+'/api/spiders', {
    params: {
      page,
    },
  }).then((result)=>result.data);
}

export function postCancelSpider(id) {
  return axios.post(base+'/api/cancel_spider', {
    id,
  }).then((result)=>result.data);
}

export function postSearchKey(r) {
  return axios.post(base+'/api/crawl', {
    ...r,
  }).then((result)=>result.data);
}

export function getKeys() {
  return axios.get(base+'/api/keys')
      .then((result)=>result.data);
}

export function databaseSearch(key, page=1, sortMethod, desc) {
  return axios.post(base+'/api/search', {
    key, page, sort_method: sortMethod, desc,
  }).then((result)=>result.data);
}

export function splitSentence(sentence) {
  return axios.get(base+'/api/split', {
    params: {
      sentence,
    },
  }).then((result)=>result.data).then((data)=>{
    if (data.code!=='0') {
      return [sentence];
    } else {
      return data.data.word;
    }
  });
}

export function getComments(isbn) {
  return axios.get(base+'/api/comments', {
    params: {
      isbn,
    },
  }).then((result)=>result.data);
}


export function getWordCloud(isbn) {
  return axios.get(base+'/api/word_cloud', {
    params: {
      isbn,
    },
  }).then((result)=>result.data);
}


export function getProductInfo(isbn) {
  return axios.get(base+'/api/product_info', {
    params: {
      isbn,
    },
  }).then((result)=>result.data);
}

export function getStatistics(key) {
  return axios.post(base+'/api/search_statistics', {
    key,
  }).then((result)=>result.data);
}
